﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

namespace Vincent
{
    /// <summary>
    /// 系统错误操作类
    /// </summary>
    public class _Log
    {
        public _Log()
        {
        }

        #region 系统异常捕获

        /// <summary>
        /// 保存异常信息到文件
        /// </summary>
        /// <param name="e"></param>    
        public static void SaveMessage(string e)
        {
            string root = AppDomain.CurrentDomain.BaseDirectory;
            string filename = DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "").Replace("/", "");
            string filePath = root + "Log\\" + filename + ".txt";
            bool isTrue = CreateDirectory(root + "Log\\");  // 检查文件目录是否存在

            if (isTrue)
            {
                StreamWriter w = new StreamWriter(filePath, true, Encoding.Default);
                w.WriteLine(e + "(" + DateTime.Now.ToString() + ")\r\n");
                w.Flush();
                w.Close();
            }
        }

        /// <summary>
        /// 文件目录是否存在，不存在则新建
        /// </summary>
        /// <param name="path"> 决对路径,如:e:soft\log\</param>
        /// <returns></returns>
        private static bool CreateDirectory(string path)
        {
            string aa = Directory.GetDirectoryRoot(path);
            if (!Directory.Exists(Directory.GetDirectoryRoot(path)))
            {
                return false;
            }

            try
            {
                Directory.CreateDirectory(path);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

    }
}
